﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Mime;
using System.Text;
using System.Threading.Tasks;
using static LightControllerDll.CstClassLibControllerDll;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TrackBar;

namespace CLVisionLightControlTS
{
    internal class CSTLight : AbstractLightDevice
    {
        public static int Rs232_or_Ethernet = Rs232Mode; //EthernetMode
        public static int connect_state = 0;

        /// <summary>
        /// 初始化，连接光源
        /// </summary>
        /// <param name="IP"></param>
        /// <param name="iPort"></param>
        /// <param name="DataSocket"></param>
        /// <param name="ErrorCode"></param>
        /// <param name="ErrorText"></param>
        /// <param name="Occurred"></param>
        /// <returns></returns>
        public override bool Init(string IP, int iPort, out uint DataSocket, out int ErrorCode, out string ErrorText, out bool Occurred)
        {
            ErrorCode = 0;
            ErrorText = "succese";
            Occurred = false;
            int ControllerHandle = 0;
            int Ret = ConnectIP(IP, 1, ref ControllerHandle);
            if (Ret == SUCCESS)
            {
                DataSocket = (uint)ControllerHandle;
                Rs232_or_Ethernet = EthernetMode;
                connect_state = 1;
            }
            else
            {
                ErrorCode = -1;
                ErrorText = "IP连接控制器失败，请检查光源控制软件是否已经连接该IP，或者检查网线！";
                Occurred = true;
                DataSocket = 0;
                return false;
            }
            // 第二个参数 1:在线，2：脱机
            Ret = SetDebugMode(Rs232_or_Ethernet, 2, ControllerHandle);
            if (Ret != SUCCESS)
            {
                ErrorCode = -1;
                ErrorText = "设置控制器在线/脱机模式失败";
                Occurred = true;
                DataSocket = 0;
                return false;
            }
            return true;
        }

        /// <summary>
        /// 释放，断开光源
        /// </summary>
        /// <param name="DataSocket"></param>
        /// <param name="ErrorCode"></param>
        /// <param name="ErrorText"></param>
        /// <param name="Occurred"></param>
        /// <returns></returns>
        public override bool DeInit(uint DataSocket, out int ErrorCode, out string ErrorText, out bool Occurred)
        {
            ErrorCode = 0;
            ErrorText = "succese";
            Occurred = false;
            if (SetInternalTriggerMode(Rs232_or_Ethernet, 3, 0, (int)DataSocket) != SUCCESS)
            {
                ErrorCode = -1;
                ErrorText = "停止流程失败";
                Occurred = true;
                return false;
            }
            int Ret = DestroyIpConnection((int)DataSocket); //断开IP连接
            if (Ret == SUCCESS)
            {
                connect_state = 0;
            }
            else
            {
                ErrorCode = -1;
                ErrorText = "断开IP失败";
                Occurred = true;
                return false;
            }
            return true;
        }

        /// <summary>
        /// 设置触发模式
        /// </summary>
        /// <param name="DataSocket"></param>
        /// <param name="Mode"></param>
        /// <param name="ErrorCode"></param>
        /// <param name="ErrorText"></param>
        /// <param name="Occurred"></param>
        /// <returns></returns>
        public override bool TriggerMode(uint DataSocket, int Mode, out int ErrorCode, out string ErrorText, out bool Occurred)
        {
            ErrorCode = 0;
            ErrorText = "success";
            Occurred = false;
            int Ret = SetTriggerMode(Rs232_or_Ethernet, Mode, (int)DataSocket);
            if (Ret != SUCCESS)
            {
                if (Mode == 1)
                {
                    ErrorText = $"设置外触发模式失败";
                    ErrorCode = 101;
                    Occurred = true;
                    return false;
                }
                else if (Mode == 2)
                {
                    ErrorText = $"设置内触发模式失败";
                    ErrorCode = 102;
                    Occurred = true;
                    return false;
                }
                else if (Mode == 3)
                {
                    ErrorText = $"设置常亮模式失败";
                    ErrorCode = 103;
                    Occurred = true;
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// 打开光源
        /// </summary>
        /// <param name="DataSocket"></param>
        /// <param name="LightData"></param>
        /// <param name="LightTime"></param>
        /// <param name="ErrorCode"></param>
        /// <param name="ErrorText"></param>
        /// <param name="Occurred"></param>
        /// <returns></returns>
        public override bool OpenLight(uint DataSocket, int[] LightData, int LightTime, out int ErrorCode, out string ErrorText, out bool Occurred)
        {
            ErrorCode = 0;
            ErrorText = "success";
            Occurred = false;
            //if (connect_state == 1)
            //{
            //    return true;
            //}
            int Ret = SetConstantLightModeData(Rs232_or_Ethernet, LightData, (int)DataSocket);
            if (Ret != SUCCESS)
            {
                Occurred = true;
                ErrorCode = -1;
                ErrorText = "设置常亮参数失败";
                return false;
            }
            Ret = SetConstantLightTime(Rs232_or_Ethernet, 2, LightTime, (int)DataSocket);
            if (Ret != SUCCESS)
            {
                Occurred = true;
                ErrorCode = -1;
                ErrorText = "设置开始点亮失败";
                return false;
            }
            return true;
        }

        /// <summary>
        /// 关闭光源
        /// </summary>
        /// <param name="DataSocket"></param>
        /// <param name="LightTime"></param>
        /// <param name="ErrorCode"></param>
        /// <param name="ErrorText"></param>
        /// <param name="Occurred"></param>
        /// <returns></returns>
        public override bool CloseLight(uint DataSocket, int LightTime, out int ErrorCode, out string ErrorText, out bool Occurred)
        {
            ErrorCode = 0;
            ErrorText = "success";
            Occurred = false;
            //if (connect_state == 1)
            //{
            //    return true;
            //}
            int Ret = SetConstantLightTime(Rs232_or_Ethernet, 3, LightTime, (int)DataSocket);
            if (Ret != SUCCESS)
            {
                Occurred = true;
                ErrorCode = -1;
                ErrorText = "熄灭失败";
                return false;
            }
            return true;
        }

        /// <summary>
        /// 在线/离线模式
        /// </summary>
        /// <param name="DataSocket"></param>
        /// <param name="offlineOrOnline"></param>
        /// <param name="ErrorCode"></param>
        /// <param name="ErrorText"></param>
        /// <param name="Occurred"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public override bool OfflineOrOnline(uint DataSocket, bool offlineOrOnline, out int ErrorCode, out string ErrorText, out bool Occurred)
        {
            throw new NotImplementedException();
        }
    }
}
